package com.atguigu.eduservice.controller.front;


import com.atguigu.common.R;
import com.atguigu.common.utils.JwtUtils;
import com.atguigu.common.vo.CommonCourseInfoVo;
import com.atguigu.eduservice.client.OrdersClient;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.chapvdeovo.ChapterVo;
import com.atguigu.eduservice.entity.coursevo.FrontCourseInfoVo;
import com.atguigu.eduservice.entity.coursevo.FrontCourseVo;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程前台显示接口
 * </p>
 *
 * @author testjava
 * @since 2020-04-13
 */
@RestController
@RequestMapping("/eduservice/frontcourse")
@Api(description="课程首页")
//@CrossOrigin
public class FrontEduCourseController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrdersClient ordersClient;

    //1 条件查询分页课程列表
    @PostMapping("getFrontCourseList/{page}/{limit}")
    @ApiOperation(value = "获取所有课程信息并分页")
    public R getFrontCourseList(@PathVariable long page,
                                @PathVariable long limit,
                                @RequestBody(required = false) FrontCourseVo frontCourseVo) {
        Page<EduCourse> pageParam = new Page<>(page,limit);
        Map<String, Object> map = courseService.getCourseFrontList(pageParam,frontCourseVo);
        return R.ok().data(map);
    }

    //2 课程详情
    @GetMapping("getFrontCourseInfo/{courseId}")
    @ApiOperation(value = "点击课程图片就能获取id并根据id得知课程详情")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request) {
        //根据课程id查询课程基本信息，用于详情显示
        FrontCourseInfoVo frontCourseInfoVo = courseService.getCourseBaseInfoFront(courseId);
        //根据课程id查询章节和小节
        List<ChapterVo> chapterVideoList = chapterService.getCourseChapterVideo(courseId);

        //远程调用，查询用户是否购买过课程
        boolean buyCourse = ordersClient.isBuyCourse(JwtUtils.getMemberIdByJwtToken(request), courseId);

        return R.ok().data("courseInfo",frontCourseInfoVo).data("chapterVideoList",chapterVideoList).data("isbuy",buyCourse);
    }

    //根据课程id查询课程信息，为了生成订单使用
    @GetMapping("getCommonCourseInfo/{courseId}")
    public CommonCourseInfoVo getCommonCourseInfo(@PathVariable String courseId) {
        //根据课程id查询课程基本信息，用于详情显示
        FrontCourseInfoVo frontCourseInfoVo = courseService.getCourseBaseInfoFront(courseId);
        CommonCourseInfoVo commonCourseInfoVo = new CommonCourseInfoVo();
        BeanUtils.copyProperties(frontCourseInfoVo,commonCourseInfoVo);
        return commonCourseInfoVo;
    }

}

